package mysql

import (
	"fresh-box/freshrpc/freshrpc"
	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	Tid          int     `gorm:"type:int(11);not null;comment:类型id"`
	FreshName    string  `gorm:"type:varchar(15);comment:商品名称"`
	FreshContent string  `gorm:"type:varchar(15);comment:商品简介"`
	FreshPrice   float64 `gorm:"type:decimal(10,2);comment:商品价格"`
}

func (f *Fresh) FreshAdd(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}

func (f *Fresh) FindFreshByFreshName(db *gorm.DB, name string) error {
	return db.Debug().Where("fresh_name=?", name).First(f).Error
}

func (f *Fresh) FreshDetail(db *gorm.DB, id int64) *gorm.DB {
	var list *freshrpc.FreshList
	return db.Debug().Model(&Fresh{}).
		Select(
			"freshes.id fresh_id",
			"goods_types.name type_name",
			"freshes.fresh_name fresh_name",
			"freshes.fresh_content fresh_content",
			"freshes.fresh_price fresh_price",
		).
		Joins("LEFT JOIN goods_types ON freshes.tid = goods_types.id").
		Where("freshes.id=?", id).
		Find(&list)
}
func FreshList(db *gorm.DB, in *freshrpc.FreshListRequest) []*freshrpc.FreshList {
	var list []*freshrpc.FreshList
	db.Debug().Model(&Fresh{}).
		Select(
			"freshes.id fresh_id",
			"goods_types.name type_name",
			"freshes.fresh_name fresh_name",
			"freshes.fresh_content fresh_content",
			"freshes.fresh_price fresh_price",
		).
		Joins("LEFT JOIN goods_types ON freshes.tid = goods_types.id").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list

}
